﻿@using Freesql.Tool.Helpers;
@{
    var gen = Model as Freesql.Tool.Models.RazorAllTemplateModel;
    var list = gen.Apis;
    var names = new List<string>();
}
import api from '@@/api/helper'
@foreach (var item in list)
    {
        var input = string.Empty;
        var para = "{}";
        if (!string.IsNullOrEmpty(item.ArgType))
        {
            input = $"{item.ArgName}:{item.ArgType}";
            para = item.ArgName;
        }
        else
        {
            input = item.ArgName;
            para = "{}";
        }

        if (names.Contains(item.Name))
        {
            item.Name = item.Type + ReflectionHelper.ToUpperFirst(item.Name);
        }
        else
            names.Add(item.Name);

        if(!string.IsNullOrEmpty(input) && item.Type=="get" && para == "{}")
        {

            var paras = input.Split(",").Select(p => p.Split(':')[0]);
            paras = paras.Where(p => item.Path.IndexOf("${" + p + "}") == -1);
            paras = paras.Select(p => $"'{p}':{p}");

            if(paras.Count() > 0)
                para = "{ " + $"{string.Join(",", paras)}" + " }";
        }
@:/**
@:* @item.Desc
@if (!string.IsNullOrEmpty(item.ArgName))
{
    foreach(var arg in item.ArgName.Split(","))
    {
@:* @@param @arg
    }

}
@:* @@returns
@:*/
@:export const @ReflectionHelper.ToLowerFirst(item.Name) = async(@input) => {
    @:var result = await api.@item.Type<@item.Out>(`@item.Path.Replace(gen.Prefix,"")`, @para);
    @:if (api.check(result))
    @:{
        @:return result.data;
    @:}
    @:return null;
@:}

}
